package com.sxbbc.common.basics.files;

import java.io.InputStream;

import common.Logger;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.sxbbc.common.basics.enums.RandNumType;
import com.sxbbc.common.basics.files.qiniu.Auth;
import com.sxbbc.common.basics.files.qiniu.Configuration;
import com.sxbbc.common.basics.files.qiniu.QiniuException;
import com.sxbbc.common.basics.files.qiniu.Response;
import com.sxbbc.common.basics.files.qiniu.UploadManager;
import com.sxbbc.common.basics.files.qiniu.Zone;
import com.sxbbc.common.basics.service.config.Config;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.qr.QRCodeUtil;
import com.sxbbc.common.basics.utils.radoms.RandNumUtils;

/**

/**
 * 文件上传
 * @author arno
 *
 */
public class UploadUtils {

	private static UploadUtils uploadUtils = null;
	private static Auth auth=null;
	private static Zone z=null;
	private static Configuration c =null;
	private static UploadManager uploadManager=null;

	private Logger logger = Logger.getLogger(UploadUtils.class);

	public static UploadUtils getInstance() {
		if (uploadUtils == null) {
			synchronized (UploadUtils.class) {
				if (uploadUtils == null) {
					uploadUtils = new UploadUtils();
					z = Zone.autoZone();//自动识别存储区域
					c= new Configuration(z);
					uploadManager = new UploadManager(c);
					auth= Auth.create(Config.properties.qiniuAk, Config.properties.qiniuSk);
				}
			}
		}
		return uploadUtils;
	}

	/**
	 * 文件上传
	 * @param multipartFile
	 */
	public RestUploadFileInfo uploadFileQN(MultipartFile multipartFile) {
		try {
			if(multipartFile==null) {
				return RestUploadFileInfo.getInstance().getFail("请选择文件");
			}
			String filename=multipartFile.getOriginalFilename();
			String mime=filename.substring(filename.lastIndexOf("."), filename.length());
			//调用put方法上传
			String token = getUpToken();//获取 token
			String key=DateUtil.getInstance().currentTimeMillis()+RandNumUtils.getInstance().get(RandNumType.NUMBER, 5)+mime;
			Response res = uploadManager.put(multipartFile.getInputStream(), key, token, null, mime);
			if(res.statusCode!=200 || !StringUtils.isEmpty(res.error)) {
				return RestUploadFileInfo.getInstance().getFail(res.error);
			}
			String url=Config.properties.getDomain()+key;
			return RestUploadFileInfo.getInstance().getSuccess("成功",url);
		} catch (QiniuException e) {
			Response r = e.response;
			// 请求失败时打印的异常的信息
			logger.error("上传图片失败",e);
			return RestUploadFileInfo.getInstance().getFail(r.toString());
		}catch (Exception e) {
			// 请求失败时打印的异常的信息
			logger.error("上传图片失败",e);
			return RestUploadFileInfo.getInstance().getFail("上传失败");
		}
	}

	/**
	 * 文件上传
	 * @param inputStream 流
	 * @param filename 文件么包含后缀
	 * @return
	 */
	public RestUploadFileInfo uploadFile(InputStream inputStream,String filename ) {
		try {
			if(inputStream==null) {
				return RestUploadFileInfo.getInstance().getFail("请选择文件");
			}
			if(StringUtils.isEmpty(filename)) {
				return RestUploadFileInfo.getInstance().getFail("文件么不能为空");
			}
			String mime=filename.substring(filename.lastIndexOf("."), filename.length());
			//调用put方法上传

			String token = getUpToken();//获取 token
			String key=DateUtil.getInstance().currentTimeMillis()+RandNumUtils.getInstance().get(RandNumType.NUMBER, 5);
			Response res = uploadManager.put(inputStream, key, token, null, mime);
			if(res.statusCode!=200 || !StringUtils.isEmpty(res.error)) {
				return RestUploadFileInfo.getInstance().getFail(res.error);
			}
			String url=Config.properties.getDomain()+key;
			return RestUploadFileInfo.getInstance().getSuccess("成功",url);
		} catch (QiniuException e) {
			Response r = e.response;
			// 请求失败时打印的异常的信息
			return RestUploadFileInfo.getInstance().getFail(r.toString());
		}catch (Exception e) {
			// 请求失败时打印的异常的信息
			return RestUploadFileInfo.getInstance().getFail("上传失败");
		}
	}

	/**
	 * 生成二维码
	 * @param content 二维码内容
	 * @return
	 */
	public RestUploadFileInfo uploadQr(String content) {
		try {
			getInstance();
			String key=DateUtil.getInstance().currentTimeMillis()+RandNumUtils.getInstance().get(RandNumType.NUMBER, 5);
			String filename=key+"."+QRCodeUtil.getInstance().FORMAT_NAME;
			InputStream inputStream=QRCodeUtil.getInstance().encode(content);
			if(inputStream==null) {
				return RestUploadFileInfo.getInstance().getFail("请选择文件");
			}
			if(StringUtils.isEmpty(filename)) {
				return RestUploadFileInfo.getInstance().getFail("文件么不能为空");
			}
			String mime=filename.substring(filename.lastIndexOf("."), filename.length());
			//调用put方法上传
			String token = getUpToken();//获取 token
			Response res = uploadManager.put(inputStream, key, token, null, mime);
			if(res.statusCode!=200 || !StringUtils.isEmpty(res.error)) {
				return RestUploadFileInfo.getInstance().getFail(res.error);
			}
			String url=Config.properties.getDomain()+key;
			return RestUploadFileInfo.getInstance().getSuccess("成功",url);
		} catch (QiniuException e) {
			Response r = e.response;
			// 请求失败时打印的异常的信息
			return RestUploadFileInfo.getInstance().getFail(r.toString());
		}catch (Exception e) {
			// 请求失败时打印的异常的信息
			return RestUploadFileInfo.getInstance().getFail("上传失败");
		}
	}

	/**
	 * 获取上传链接
	 *
	 * @return
	 */
	//简单上传，使用默认策略，只需要设置上传的空间名就可以了
	public String getUpToken() {
		return auth.uploadToken(Config.properties.bucket);
	}
}
